import requests

class FileLoader:
    def __init__(self, download_url):
        self.download_url = download_url

    def load_once(self, filename: str) -> bytes:
        response = requests.get(f"{self.download_url}/{filename}")

        if response.status_code == 200:
            return response.content
        else:
            raise FileNotFoundError("File not found")

    def load_stream(self, filename: str):
        response = requests.get(f"{self.download_url}/{filename}", stream=True)

        if response.status_code == 200:
            for chunk in response.iter_content(chunk_size=4096):  # 每次读取4KB
                yield chunk
        else:
            raise FileNotFoundError("File not found")

# 示例用法
if __name__ == "__main__":
    loader = FileLoader(download_url="http://127.0.0.1:8000/download")
    try:
        data = loader.load_once("file.txt")
        print("File loaded successfully!")
    except FileNotFoundError as e:
        print(e)


    loader = FileLoader(download_url="http://127.0.0.1:8000/download")
    try:
        for data in loader.load_stream("file.txt"):
            print(data)  # 处理每个数据块
    except FileNotFoundError as e:
        print(e)